package com.example.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import org.litepal.crud.DataSupport;

import java.util.List;

/**
 * Created by YC_chen on 2017/6/22.
 */

public class InvestigationAdapter extends ArrayAdapter implements View.OnClickListener, RadioGroup.OnCheckedChangeListener, CompoundButton.OnCheckedChangeListener {
    private Activity activity;
    private int resourceId;
    private List<Investigation> list;
    private int mPosition;
    private Investigation investigation;

    public Investigation getInvestigation() {
        return investigation;
    }

    public int getmPosition() {
        return mPosition;
    }

    public void setmPosition(int mPosition) {
        this.mPosition = mPosition;
    }


    public List<Investigation> getList() {
        return list;
    }

    public void setList(List<Investigation> list) {
        this.list = list;
    }


    public InvestigationAdapter(Context context, int resourceId, List<Investigation> list) {
        super(context, resourceId, list);
        this.activity = (Activity) context;
        this.resourceId = resourceId;
        this.list = list;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        investigation = list.get(position);
        mPosition = position;
        View view;
        final ViewHolder holder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            holder = new ViewHolder();
            holder.thirdIndex = (TextView) view.findViewById(R.id.tv_third_index);
            holder.investigationStandard = (TextView) view.findViewById(R.id.tv_investigation_standard);
            if (investigation.getTestWay().equals("0")) {
                holder.testResult = (RadioGroup) view.findViewById(R.id.rg_test_result0);
                holder.testResult.setVisibility(View.VISIBLE);
            } else if (investigation.getTestWay().equals("1")) {
                holder.testResult = (RadioGroup) view.findViewById(R.id.rg_test_result1);
                holder.testResult.setVisibility(View.VISIBLE);
            }

            holder.record = (EditText) view.findViewById(R.id.edit_record);
            holder.save = (Button) view.findViewById(R.id.btn_save);
            holder.save.setOnClickListener(this);
            holder.save.setTag(position);
            holder.save.setTag(R.id.edit_record, holder.record);
            holder.picture = (TextView) view.findViewById(R.id.tv_picture);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        holder.thirdIndex.setText(investigation.getThirdIndex());
        holder.investigationStandard.setText(investigation.getInvestigationStandard());
        holder.testResult.setTag(position);
        holder.testResult.setTag(R.id.edit_record, holder.record);
        holder.testResult.setOnCheckedChangeListener(this);
        holder.picture.setOnClickListener(this);
        holder.save.setOnClickListener(this);
        holder.picture.setTag(position);
        int num = getPictureNum(list.get(position).getPoint(), list.get(position).getAddress(), list.get(position).getStandard(), list.get(position).getCode(), list.get(position).getThirdIndex());
        if (num > 0) holder.picture.setText("已选择" + num + "张");
        else holder.picture.setText("选择照片");
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_picture:
                Intent intent = new Intent(activity, PhotoPickerActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("point", list.get((int) v.getTag()).getPoint());
                bundle.putString("address", list.get((int) v.getTag()).getAddress());
                bundle.putString("thirdIndex", list.get((int) v.getTag()).getThirdIndex());
                bundle.putString("standard", list.get((int) v.getTag()).getStandard());
                bundle.putString("code", list.get((int) v.getTag()).getCode());
                bundle.putString("picture", list.get((int) v.getTag()).getPicture());
                bundle.putInt("position", (int) v.getTag());
                Log.d("investigationPosition", String.valueOf(v.getTag()));
                intent.putExtras(bundle);
                activity.startActivityForResult(intent, 0);
                break;
            case R.id.btn_save:
                //更新数据库加检查
                int position = (int) v.getTag();
                EditText record = (EditText) v.getTag(R.id.edit_record);
                if (list.get(position).getTestResult().equals("0")) {
                    if (!record.getText().toString().equals("")) {
                        Toast.makeText(activity, "选择符合不能填写记录", Toast.LENGTH_SHORT).show();
                    } else {
                        list.get(position).setRecord(record.getText().toString());
                        list.get(position).save();
                        Toast.makeText(activity, "测评成功", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    if (record.getText().toString().equals("")) {
                        Toast.makeText(activity, "未填写记录", Toast.LENGTH_SHORT).show();
                    } else {
                        list.get(position).setRecord(record.getText().toString());
                        list.get(position).save();
                        Toast.makeText(activity, "测评成功", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
    }


    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        int position = (int) group.getTag();
        switch (group.getId()) {
            case R.id.rg_test_result0:
                if (checkedId == R.id.rb_0) {
                    list.get(position).setTestResult("0");
                } else if (checkedId == R.id.rb_1) {
                    list.get(position).setTestResult("1");
                }
                break;
            case R.id.rg_test_result1:
                if (checkedId == R.id.rb_2) {
                    list.get(position).setTestResult("0");
                } else if (checkedId == R.id.rb_3) {
                    list.get(position).setTestResult("1");
                } else if (checkedId == R.id.rb_4) {
                    list.get(position).setTestResult("2");
                }
                break;
        }
    }

    private int getPictureNum(String point, String address, String standard, String code, String thirdIndex) {
        List<Investigation> investigations = DataSupport.select("picture")
                .where("point = ? and address = ? and standard = ? and code = ? and thirdIndex = ?", point, address, standard, code, thirdIndex)
                .find(Investigation.class);

        String.valueOf(investigations.get(0).getPicture());
        String pictures = investigations.get(0).getPicture();
        String myString = new String(pictures);
        Log.d("mymString", myString);
        int start = 0;
        int num = 0;
        //获取图片名串中的图片数量
        while (start != pictures.length()) {
            myString = myString.substring(start);
            Log.d("mymmString", myString);
            Log.d("myStart", String.valueOf(start));
            PhotoInfo photoInfo = new PhotoInfo();
            photoInfo.setChecked(false);
            /*String photoName="";*/
            Log.d("myString,", String.valueOf(myString.indexOf("/r/n")));
            if (myString.indexOf("/r/n") < 0) break;
            /*photoName=myString.substring(0,myString.indexOf("/r/n"));*/
            /*Log.d("myPhotoName",photoName);*/
            num++;
           /* photoInfo.setUrl(Environment.getExternalStorageDirectory()
                    .toString() + "/"+areaCode+"/"+photoName);
            mGridAdapter.getList().add(photoInfo);*/
            start = myString.indexOf("/r/n") + 4;
        }
        return num;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        EditText record = (EditText) buttonView.getTag();
        switch (buttonView.getId()) {
            case R.id.rb_0:
                if (isChecked) {
                    Log.d("TAG", "rb0");
                    //   record.setVisibility(View.GONE);
                    Log.d("TAG", buttonView.toString());
                }
                break;
            case R.id.rb_1:
                if (isChecked) {
                    Log.d("TAG", "rb1");
                    //   record.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.rb_2:
                if (isChecked) {
                    Log.d("TAG", "rb2");
                    // record.setVisibility(View.GONE);
                }
                break;
            case R.id.rb_3:
                if (isChecked) {
                    Log.d("TAG", "rb3");
                    //  record.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.rb_4:
                if (isChecked) {
                    Log.d("TAG", "rb4");
                    //   record.setVisibility(View.VISIBLE);
                }
                break;
        }
    }


    class ViewHolder {
        TextView thirdIndex;
        TextView investigationStandard;
        RadioGroup testResult;
        EditText record;
        TextView picture;
        Button save;
    }
}
